<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="naogen" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 放置一个el-tree组件 -->
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon
                  icon-class="user"
                /> <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog

        v-if="showDialog"
        :title=" isEdit ? '编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <addor-edit
          :id="curId"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog= false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>

import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddorEdit from './deptDialog.vue'

export default {
  components: { AddorEdit },
  data() {
    return {
      showDialog: false, // 控制显示隐藏
      list: [], // 依赖一份树形数据
      curId: '', // 添加功能存放的id
      isEdit: false, // 判断编辑还是添加
      originList: [],
      aaa: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        // 保存数据
        const res = await getDepartments()
        // 删除第一个数据
        // res.data.depts.shift()
        // console.log('res', res)
        // this.aaa = res
        // this.list = tranListToTreeData(res.data.depts)
        // map循环遍历数据 获得一个新的数据对象
        // this.aaa = res.data.depts.map((item) => {
        //   return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        // })
        // console.log(this.originList)
        // 将平铺数据转化为树形数据
        // debugger
        res.data.depts.shift()
        // console.log(res.data.depts)
        // console.log(tranListToTreeData(res.data.depts))
        this.list = tranListToTreeData(res.data.depts)
        this.originList = this.list.map((item) => {
          return { id: item.id, pid: item.pid, code: item.code, name: item.name }
        })
        // this.list = [this.originList]
      } catch (error) {
        console.log(error)
      }
    },
    // 点击添加子部门 保存id
    hAdd(id) {
      // 点击后显示弹层
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id
      this.isEdit = false
    },
    hSuccess() {
      this.showDialog = false
      this.loadDepartments()
    },
    // 实现编辑
    hEdit(id) {
      // 显示弹层
      this.showDialog = true
      this.isEdit = true
      this.curId = id
    },
    // 实现删除
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log(res, 'delDepartment')
        // 更新数据
        this.loadDepartments()
      } catch (error) {
        console.log(error)
      }
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {

      })
    }
  }
}
</script>
